package com.findviewbydream.evil.eda.Answer;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.findviewbydream.evil.eda.Activity.ImageZoomActivity;
import com.findviewbydream.evil.eda.Bean.AnswerBean;
import com.findviewbydream.evil.eda.Data.Data;
import com.findviewbydream.evil.eda.R;
import com.findviewbydream.evil.eda.Utils.Utils;
import com.github.ybq.android.spinkit.SpinKitView;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.ArrayList;
import java.util.Collections;

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda.Answer
 * 时间：16-8-25 下午6:10
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：回答问题 界面 Fragment
 ********************************************/
public class AnswerFragment extends Fragment
{
    private static final String URL = Data.IP + "details/";

    private View view;

    private ImageView imageView;
    private TextView title;
    private RadioButton[] radioButton = new RadioButton[4];

    private AnswerBean bean;
    private int position;

    private String yes;/*保存正确答案，方便判定*/
    private WebView webView;
    private SpinKitView loading;
    private TextView error;
    private CardView cardView4;

    public static AnswerFragment newInstance(AnswerBean bean, int position)
    {
        AnswerFragment answerFragment = new AnswerFragment();
        answerFragment.bean = bean;
        answerFragment.position = position;
        return answerFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.answer_fragment, container, false);

        initView(view);
        initData();
        onClick();
        setWebView();

        return view;
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    private void initView(View view)
    {
        imageView = (ImageView) view.findViewById(R.id.imageView);
        title = (TextView) view.findViewById(R.id.title);
        radioButton[0] = (RadioButton) view.findViewById(R.id.radioButton1);
        radioButton[1] = (RadioButton) view.findViewById(R.id.radioButton2);
        radioButton[2] = (RadioButton) view.findViewById(R.id.radioButton3);
        radioButton[3] = (RadioButton) view.findViewById(R.id.radioButton4);
        webView = (WebView) view.findViewById(R.id.webView);
        loading = (SpinKitView) view.findViewById(R.id.loading);
        error = (TextView) view.findViewById(R.id.error);
        cardView4 = (CardView) view.findViewById(R.id.cardView4);
    }

    /**
     * 初始化数据
     */
    private void initData()
    {
        if (bean.getImg() != null)
            Glide.with(getActivity()).load(Utils.getNetImg(bean.getImg())).into(imageView);
        else
            imageView.setVisibility(View.GONE);


        title.setText(bean.getTilte());


        ArrayList<String> choose_List = new ArrayList<>();
        choose_List.add(bean.getYes());
        yes = bean.getYes();
        Collections.addAll(choose_List, bean.getNo());
        Collections.shuffle(choose_List);//将arraylist数组乱序排列

        for (int i = 0; i < radioButton.length; i++)
        {
            radioButton[i].setText(choose_List.get(i));
        }

        if (!bean.getUrl().equals(""))
        {
            new Handler().postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    webView.loadUrl(URL + bean.getUrl());/*最后加载网页*/
                }
            }, 300);
        }
        else
            cardView4.setVisibility(View.GONE);

        //webView.loadUrl("https://www.baidu.com/");
    }

    /**
     * 点击事件
     */
    private void onClick()
    {
        View.OnClickListener onClickListener = new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Button button = (Button) view;
                AnswerActivity.anwerBool[position] = button.getText().equals(yes);
            }
        };

        for (RadioButton button : radioButton)
            button.setOnClickListener(onClickListener);


        imageView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                /*if (bean.getUrl() != null)
                {
                    startActivity(new Intent(getActivity()
                            , WebActivity.class)
                            .putExtra(WebActivity.TITLE, "详情页")
                            .putExtra(WebActivity.URL, bean.getUrl()));
                }*/
                if (bean.getImg() != null)
                {
                    startActivity(new Intent(getActivity(), ImageZoomActivity.class)
                            .putExtra(ImageZoomActivity.IMGURL, bean.getImg()));
                }
            }
        });

    }


    /**
     * 设置网络浏览器
     */
    @SuppressLint("SetJavaScriptEnabled")
    private void setWebView()
    {
        WebSettings webSettings = webView.getSettings();

        /*设置适应屏幕*/
        {
            webSettings.setUseWideViewPort(true);//设置此属性，可任意比例缩放
            webSettings.setLoadWithOverviewMode(true);
        }
        /*支持缩放*/
        /*{
            webSettings.setJavaScriptEnabled(true);
            webSettings.setBuiltInZoomControls(true);
            webSettings.setSupportZoom(true);
        }*/
        /*支持手动输入*/
        {
            //webView.requestFocusFromTouch();
        }

        /*优先使用缓存*/
        {
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            // 开启 DOM storage API 功能
            webSettings.setDomStorageEnabled(true);
        }

        webView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
                if (newProgress == 100)
                {
                    loading.setVisibility(View.GONE);
                }
                else if (newProgress == -1)
                {
                    loading.setVisibility(View.GONE);
                    error.setVisibility(View.VISIBLE);
                }
            }
        });

        /*在当前页面打开新的链接*/
        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });
    }

}
